/*
 * PulpitInfoPanel.java
 *
 * Created on May 29, 2007, 12:55 PM
 */

package quizgame.admin;

import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JLabel;
import quizgame.protocol.pulpit.PulpitStatus;

/**
 *
 * @author  gsohtell
 */
public class PulpitInfoPanel extends javax.swing.JPanel {
    
    public static final int PING_CHECK_INTERVAL = 500;
    private ActiveBoardPanel abp;
    private PulpitStatus pulpetStatus;
    private String name;
    
    /**
     * Creates new form PulpitInfoPanel
     */
    public PulpitInfoPanel(final ActiveBoardPanel abp, final String name, PulpitStatus pulpetStatus) {
        this.abp = abp;
        this.name = name;
        this.pulpetStatus = pulpetStatus;
        initComponents();
        nameLabel.setText(name);
        nickTextField.setText(pulpetStatus.getNickname());
        scoreTextField.setText(pulpetStatus.getScore() + "");
        switch(pulpetStatus.getState()) {
            case ANSWERING:
                stateLabel.setText("Answering!");
                stateLabel.setBackground(Color.GREEN);
                break;

            case LOCKED:
                if(pulpetStatus.isAlwaysLocked()) {
                    stateLabel.setText("Locked - Answered wrong");
                } else {   
                    stateLabel.setText("Locked");
                }
                stateLabel.setBackground(Color.GRAY);
                break;
                
            case UNLOCKED:
                stateLabel.setText("Unlocked");
                stateLabel.setBackground(Color.BLUE);
                break;
        }
        new Timer(true).scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if(pingLabel != null && abp != null && name != null) {
                    pingLabel.setText(abp.secSinceLastPing(name));
                }
            }
        }, PING_CHECK_INTERVAL+5000, PING_CHECK_INTERVAL); // Wait for GUI...
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nickTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        scoreTextField = new javax.swing.JTextField();
        stateLabel = new javax.swing.JLabel();
        updateButton = new javax.swing.JButton();
        pingLabel = new javax.swing.JLabel();

        jLabel1.setText("Name:");

        jLabel2.setText("Nick:");

        jLabel3.setText("Score:");

        jLabel4.setText("State:");

        nickTextField.setText("---");

        nameLabel.setText("---");

        scoreTextField.setText("0");

        stateLabel.setText("---");
        stateLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        pingLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        pingLabel.setText("?");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(updateButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1)
                            .add(jLabel3)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, scoreTextField)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(nameLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(pingLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(nickTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, stateLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(nameLabel)
                    .add(pingLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(nickTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(scoreTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(stateLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(updateButton)
                .addContainerGap(13, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        abp.changePulpetNickname(name, nickTextField.getText());
        try {
            abp.changePulpetScore(name, Integer.parseInt(scoreTextField.getText()));
        } catch (NumberFormatException ex) {
            abp.showMessage("Could not parse new score for pulpit: " + scoreTextField.getText());
        }
    }//GEN-LAST:event_updateButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nickTextField;
    private javax.swing.JLabel pingLabel;
    private javax.swing.JTextField scoreTextField;
    private javax.swing.JLabel stateLabel;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
    
    public JLabel getPingLabel() {
        return pingLabel;
    }
}
